#!/bin/bash

# cloudAgent 安装脚本

# 检查是否以root权限运行
if [ "$EUID" -ne 0 ]
  then echo "请以root权限运行此脚本"
  exit
fi

# 编译程序
echo "正在编译cloudAgent..."
go build -o cloudAgent main.go

# 检查编译是否成功
if [ $? -ne 0 ]; then
    echo "编译失败"
    exit 1
fi

# 复制二进制文件到系统路径
echo "正在安装cloudAgent..."
cp cloudAgent /usr/local/bin/

# 创建配置目录
mkdir -p /etc/cloudAgent

# 创建systemd服务文件
cat > /etc/systemd/system/cloudAgent.service << EOF
[Unit]
Description=Cloud Agent Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/etc/cloudAgent
ExecStart=/usr/local/bin/cloudAgent
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

# 重新加载systemd
systemctl daemon-reload

echo "cloudAgent安装完成!"
echo "使用以下命令启动服务:"
echo "  systemctl start cloudAgent"
echo "使用以下命令设置开机自启:"
echo "  systemctl enable cloudAgent"
echo "使用以下命令查看服务状态:"
echo "  systemctl status cloudAgent"